package models

import (
	"time"

	"gorm.io/gorm"
)

// 工单状态
const (
	StatusOpen     = "open"     // 开放状态
	StatusAssigned = "assigned" // 已分配
	StatusResolved = "resolved" // 已解决
	StatusClosed   = "closed"   // 已关闭
)

// 工单优先级
const (
	PriorityLow    = "low"    // 低优先级
	PriorityMedium = "medium" // 中优先级
	PriorityHigh   = "high"   // 高优先级
)

// Ticket 工单模型
type Ticket struct {
	ID          uint           `json:"id" gorm:"primaryKey"`
	Title       string         `json:"title" gorm:"type:varchar(255);not null"`
	Description string         `json:"description" gorm:"type:text"`
	Status      string         `json:"status" gorm:"type:varchar(20);default:open"`
	Priority    string         `json:"priority" gorm:"type:varchar(20);default:medium"`
	CreatorID   uint           `json:"creator_id" gorm:"not null"`
	Creator     User           `json:"creator" gorm:"foreignKey:CreatorID"`
	AssigneeID  *uint          `json:"assignee_id"`
	Assignee    *User          `json:"assignee" gorm:"foreignKey:AssigneeID"`
	AssignedAt  *time.Time     `json:"assigned_at"`
	ResolvedAt  *time.Time     `json:"resolved_at"`
	ClosedAt    *time.Time     `json:"closed_at"`
	HandlingTime int64         `json:"handling_time" gorm:"default:0"` // 处理时长(秒)
	CreatedAt   time.Time      `json:"created_at"`
	UpdatedAt   time.Time      `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `json:"-" gorm:"index"`
	Comments    []Comment      `json:"comments" gorm:"foreignKey:TicketID"`
}

// TableName 设置表名
func (Ticket) TableName() string {
	return "tickets"
}

// Comment 工单评论模型
type Comment struct {
	ID        uint           `json:"id" gorm:"primaryKey"`
	Content   string         `json:"content" gorm:"type:text;not null"`
	TicketID  uint           `json:"ticket_id" gorm:"not null"`
	UserID    uint           `json:"user_id" gorm:"not null"`
	User      User           `json:"user" gorm:"foreignKey:UserID"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}

// TableName 设置表名
func (Comment) TableName() string {
	return "comments"
} 